//
//  MyProfileViewController.m
//  EasyGo
//
//  Created by newhuidong on 16/1/19.
//  Copyright © 2016年 HuiDong. All rights reserved.
//

#import "MyProfileViewController.h"
#import "AddressesViewController.h"
#import "historyViewController.h"

#import "AppDelegate.h"
#import "AFNetworking.h"
#import "URL.h"

#import "MJRefresh.h"


@interface MyProfileViewController (){
    
    UIImageView *navBarHairlineImageView;
}
@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UILabel *name_label;
@property (weak, nonatomic) IBOutlet UILabel *phone_label;
@property (weak, nonatomic) IBOutlet UILabel *score_label;
@property (weak, nonatomic) IBOutlet UILabel *onWork;
@property (weak, nonatomic) IBOutlet UILabel *offWork;

@end

@implementation MyProfileViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    

    self.title=@"我的资料";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.headImageView.layer.cornerRadius=45;
    self.headImageView.layer.masksToBounds=YES;

    self.navigationController.navigationBar.tintColor=[UIColor whiteColor];
    navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];

    
    
    [self createHeardView];
    [self loadData];
    [self loadRefresh];

}
-(void)loadRefresh{
    
    MJRefreshNormalHeader * header =[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self refreshData];
    }];
    [header setTitle:@"松开刷新" forState:MJRefreshStatePulling];
    [header beginRefreshing];
    self.tableView.mj_header=header;
    
}
-(void)refreshData{
    
    [self loadData];
    
}

-(void)createHeardView{
    
    CGRect frame =[UIScreen mainScreen].bounds;
    UIView * view =[[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, 50)];
    
    UIButton * button =[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(10, 0, frame.size.width/3-10, 30);
    
    
    /*
     常用地址
     */
    [button setTitle:@"常用地址" forState:0];
    [button setTitleColor:[UIColor grayColor] forState:0];
    button.titleLabel.font=[UIFont systemFontOfSize:15];
    [button setBackgroundImage:[UIImage imageNamed:@"lowleft"] forState:0];
    [button addTarget:self action:@selector(address:) forControlEvents:UIControlEventTouchUpInside];
    
    
    /*
     我要接单
     */
    
    UIButton * button1=[UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame=CGRectMake(frame.size.width/3, 0, frame.size.width/3, 30);
    [button1 setBackgroundImage:[UIImage imageNamed:@"lowmid"] forState:0];
    [button1 setTitle:@"我要接单" forState:0];
    button1.titleLabel.font=[UIFont systemFontOfSize:15];
    [button1 setTitleColor:[UIColor whiteColor] forState:0];

    
    /*
     历史订单
     */
    UIButton *button2=[UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame=CGRectMake((frame.size.width/3)*2, 0, frame.size.width/3-10, 30);
    [button2 setTitle:@"历史订单" forState:0];
    button2.titleLabel.font=[UIFont systemFontOfSize:15];
    [button2 setBackgroundImage:[UIImage imageNamed:@"lowright"] forState:0];
    [button2 setTitleColor:[UIColor grayColor] forState:0];
    [button2 addTarget:self action:@selector(historyButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [view addSubview:button];
    [view addSubview:button1];
    [view addSubview:button2];
    
    self.tableView.tableFooterView=view;

}
#pragma -----------常用地址-------------
-(void)address:(UIButton *)button{
    
    AddressesViewController* address=[self.storyboard instantiateViewControllerWithIdentifier:@"AddressesViewController"];
    
    [self.navigationController pushViewController:address animated:YES ];
    
    NSLog(@"常用地址");
    
}

#pragma ---------------历史订单--------------------
-(void)historyButton:(UIButton *)button{
    
    
    historyViewController* history=[[historyViewController alloc]init];
    
    [self.navigationController pushViewController:history animated:YES];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 10;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.1;
}


-(void)loadData{
    
    
    AFHTTPSessionManager * manager=[AFHTTPSessionManager manager];
    manager.responseSerializer=[AFJSONResponseSerializer serializer];
    
    NSMutableDictionary * paramert=[[NSMutableDictionary alloc]init];
    AppDelegate * temp=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    
    paramert[@"token"]=temp.token;
    
    
    NSString * urlString=[NSString stringWithFormat:@"%@%@",url1,@"GetMyMsg"];
    
    [manager POST:urlString parameters:paramert progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary * dic=(NSDictionary*)responseObject;
        
        NSDictionary * dict=dic[@"Data"];
        self.name_label.text=dict[@"Name"];
        self.phone_label.text=dict[@"PhoneNum"];
        self.phone_label.adjustsFontSizeToFitWidth=YES;
        
        self.score_label.text=[NSString stringWithFormat:@"%@",dict[@"Score"]];
        
        [self.tableView.mj_header endRefreshing];
        
        NSString * string=dic[@"Status"];
        
        int intString=[string intValue];
        
        if (intString==0) {
            
        }else if (intString==1){
            
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"Erroe:%@",error);
        
    }];
    
    
}
#pragma 隐藏导航栏下面的一条线
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    navBarHairlineImageView.hidden = YES;
    
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    navBarHairlineImageView.hidden = NO;
}


@end
